/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package biojavagoalz;

import biojavagoalz.io.OutputWriter;
import biojavagoalz.worker.WorkManager;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Class that prints the output to the writer
 * @author Tadas
 */
public class OutputWorker extends Thread {

    private OutputWriter writer;
    private WorkManager manager;
    
    private boolean finished = false;

    public OutputWorker(WorkManager manager, OutputWriter writer){
        this.manager = manager;
        this.writer = writer;
    }

    public WorkManager getWorkManager(){
        return manager;
    }

    @Override
    public void run(){
        while(getWorkManager().getActiveWorkers() > 0){
            try {
                String s = getWorkManager().getResults().poll(3, TimeUnit.SECONDS);
                if(s == null){
                    continue;
                }
                writer.print(s);
            } catch (InterruptedException ex) {
                Logger.getLogger(OutputWorker.class.getName()).log(Level.INFO, null, ex);
                break;
            }
        }

        //System.out.println(Thread.currentThread().getName()+ " writer finished!");
    }

    public void setFinished(boolean fi){
        finished = fi;
    }

}
